package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.vo.CategoryTreeVo;
import com.atguigu.gmall.product.vo.CategoryViewDTO;
import com.atguigu.gmall.product.vo.ValueSkuJsonVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author 10574
 */
@RequestMapping("/api/inner/rpc/product")
@RestController
public class SkuRpcController {

    @Resource
    BaseCategory1Service baseCategory1Service;

    @Resource
    SkuInfoService skuInfoService;

    @Resource
    SkuImageService skuImageService;

    @Resource
    SpuSaleAttrService spuSaleAttrService;

    /**
     * 1、根据三级分类id，得到整个分类的完整路径
     *
     * @param c3Id
     * @return
     */
    @GetMapping("/category/view/{c3Id}")
    public Result<CategoryViewDTO> getCategoryTreeWithC3Id(@PathVariable("c3Id") Long c3Id) {
        CategoryViewDTO categoryViewDTO = baseCategory1Service.getCategoryTreeWithC3Id(c3Id);
        return Result.ok(categoryViewDTO);
    }

    /**
     * 2、获取sku-info
     *
     * @param skuId
     * @return
     */
    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getSkuInfo(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 3、获取sku-images
     *
     * @param skuId
     * @return
     */
    @GetMapping("/skuimages/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId) {
        List<SkuImage> list = skuImageService.lambdaQuery().eq(SkuImage::getSkuId, skuId).list();
        return Result.ok(list);
    }

    /**
     * 4、获取 实时价格
     *
     * @param skuId
     * @return
     */
    @GetMapping("/skuprice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.lambdaQuery().select(SkuInfo::getPrice).eq(SkuInfo::getId, skuId).one();
        return Result.ok(skuInfo.getPrice());
    }

    /**
     * 5、获取 spu销售属性名和值 集合
     *
     * @param spuId
     * @return
     */
    @GetMapping("/skusaleattr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId,
                                                    @PathVariable("skuId") Long skuId) {
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrService.getSpuSaleAttr(spuId, skuId);
        return Result.ok(spuSaleAttrs);
    }

    /**
     * 6、获取 valuesSkuJson
     *
     * @param spuId
     * @return
     */
    @GetMapping("/valuesSkuJson/{spuId}")
    Result<String> getValuesSkuJson(@PathVariable("spuId") Long spuId) {
        String valueJson = spuSaleAttrService.getValuesSkuJson(spuId);
        return Result.ok(valueJson);
    }


}